"""
JavaScript逆向 ---> 找到提供动态内容的URL
~ 浏览器开发者工具 ---> Network ---> XHR
~ 专业的抓包工具 ---> Charles / Fiddler / Wireshark
"""
import time
from threading import Thread, current_thread

import requests


def download_picture(url):
    print(current_thread().name)
    resp = requests.get(url)
    filename = url[url.rfind('/') + 1:]
    with open(f'image360/{filename}', 'wb') as file:
        file.write(resp.content)
    time.sleep(0.5)


def main():
    resp = requests.get('https://image.so.com/zjl?ch=beauty&sn=0')
    data_dict = resp.json()
    threads = []
    start = time.time()
    for index, image_dict in enumerate(data_dict['list']):
        url = image_dict['qhimg_url']
        # download_picture(url)
        fn = functools.partial(download_picture, url)
        t = Thread(target=fn)
        threads.append(t)
        t.start()
    for t in threads:
        t.join()
    end = time.time()
    print(f'执行时间: {end - start:.3f}秒')


if __name__ == '__main__':
    main()
